<?php
class exerciseknowledge extends Model{
	/**
	 * 添加某个习题的知识点
	 * @param unknown_type $eid
	 * @param unknown_type $kid
	 */
	public function addExerciseKnowledge($eid, $kid){
		$existEk = $this->reCheck($eid, $kid);
		if ($existEk){
				$this->addAccount($existEk['ek_id']);
		}else {
			$this->insertKnowledge($eid, $kid);
		}
	}
	
	public function addAccount($ekId){
		$sql = "UPDATE `howdo`.`exercise_knowledge` SET `knows_account` = `knows_account` + 1 WHERE `exercise_knowledge`.`ek_id` = '".$ekId."'";
		return $this->update($sql);
	}
	
	
	public function insertKnowledge($eid, $kid){
		$sql = "INSERT INTO `howdo`.`exercise_knowledge` (`ek_id`, `ex_id`, `knows_id`, `knows_account`) "
				. "VALUES "
				. "(NULL, '".$eid."', '".$kid."', '1');";
		return $this->insert($sql);
	}
	/**
	 * 根据习题ID获取知识点
	 * @param unknown $eid
	 * @return Ambigous <boolean, multitype:>
	 */
	public function getKnowlistByExerciseid($eid)
	{
		$sql = "SELECT `exercise_knowledge`.*,`knowledge`.`knows_name`,`knowledge`.`knows_cont` FROM `exercise_knowledge`
				LEFT JOIN `knowledge` ON `knowledge`.`knows_id`=`exercise_knowledge`.`knows_id`
				WHERE `ex_id` = '".$eid."'";
		return $this->fetchAll($sql);
	}
	
	/**
	 * 删除某习题下的某个知识点
	 * @param unknown_type $eid
	 * @param unknown_type $kid
	 * @return resource
	 */
	public function delExerciseKnowledge($eid,$kid){
		$sql = "DELETE FROM `exercise_knowledge` WHERE `ex_id`='".$eid."' AND `knows_id`='".$kid."'";
		//echo $sql;
		return $this->del($sql);
	}
	
	/**
	 * 根据skillID获取解体技巧
	 * @param unknown $eid
	 */
	public function getKnowledgeByid($sid)
	{
		$sql = "SELECT `knows_cont` FROM `knowledge` WHERE `knows_id` = $sid";
		return $this->fetchRow($sql);
	}
	
	/**
	 * 查重
	 * @param unknown_type $eid
	 * @param unknown_type $kid
	 */
	public function reCheck($eid, $kid){
		$sql = "SELECT * FROM `exercise_knowledge` WHERE `ex_id` = '".$eid."' AND `knows_id` = '".$kid."' ";
		return $this->fetchRow($sql);
	}
	
	/**
	 * 获取父节点
	 * @param unknown_type $kid
	 */
	public function getParentId($kid){
		$sql = "SELECT `knowledge`.* FROM `knowledge` WHERE `knows_id` = '".$kid."' ";
		return $this->fetchRow($sql);
	}
}